<?php
namespace app\index\controller;

use app\common\model\Users;

class LoginAjax extends Ajax
{
	
    public function UserLogin()
	{
		if (request()->isAjax()) {
		    $data = input('post.');
			
            $rule=[
				'username'  => 'require|alphaDash|length:5,20',
				'password'  => 'require|alphaNum|length:5,20',
			];
			$message=[
				'username.require'=>'用户名不能为空',
				'username.length'=>'用户名长度必须在5到20之间',
				'password.require'=>'密码不能为空',
				'password.alphaNum'=>'密码只能字母和数字',
				'password.length'=>'密码长度必须在5到20之间',
			];
			$validate=new \think\Validate($rule,$message);
			if(!$validate->check($data)){
				$result = [
						'code' => -1,
						'msg' => $validate->getError()
					];
			}elseif(Users::checkUsername($data['username']) == true){
			$result = [
						'code' => -1,
						'msg' => '用户名不存在'
					];
			}elseif(Users::checkPass($data) == false){
				$result = [
						'code' => -1,
						'msg' => '账号或密码错误'
					];
			}elseif(Users::login($data)){
					$result = [
						'code' => 0,
						'msg' => '登录成功'
					];
			}
        }
        return $result;
	}
	
	public function UserReg()
	{
		if (request()->isAjax()) {
		$data = input('post.');
		switch (true) {
            case (!$this->geetest_check($data['geetest_challenge'], $data['geetest_validate'], $data['geetest_seccode'])):
                $result['message'] = '验证码效验失败，请重试！';
                break;
            default;
			$rule=[
				'username'  => 'require|alphaDash|length:5,20',
				'nickname'  => 'require|length:2,20',
				'password'  => 'require|alphaNum|length:5,20',
				'qq'    => 'require|number|length:4,13',
				'mail'    => 'require',
			];
			$message=[
				'username.require'=>'用户名不能为空',
				'username.length'=>'用户名长度必须在5到20之间',
				'username.alphaDash'=>'用户名只能是字母、数字和下划线_及破折号-',
				'nickname.require'=>'昵称不能为空',
				'nickname.length'=>'昵称长度必须在5到20之间',
				'password.require'=>'密码不能为空',
				'password.alphaNum'=>'密码只能字母和数字',
				'password.length'=>'密码长度必须在5到20之间',
				'qq.require'=>"QQ不能为空",
				'qq.number'=>"QQ只能数字",
				'qq.length'=>"QQ长度必须在4到13之间",
				'mail.require'=>"邮箱账号不能为空",
			];
			
			$validate=new \think\Validate($rule,$message);
			if(!$validate->check($data)){
				$result = [
							'code' => -1,
							'msg' => $validate->getError()
						];
			}elseif(Users::checkUsername($data['username']) != true){
				$result = [
							'code' => -1,
							'msg' => '用户名已被注册'
						];
			}elseif(Users::checkQq($data['qq']) != true){
				$result = [
							'code' => -1,
							'msg' => 'QQ已被其他用户绑定'
						];
			}elseif(Users::where('mail', '=', $data['mail'])->find()){
				$result = [
							'code' => -1,
							'msg' => '此邮箱已被其他用户绑定'
						];
			}elseif(Users::reg($data)){
						$result = [
							'code' => 0,
							'msg' => '注册成功'
						];
				}
					break;
			}
		}
        return $result;
    }
	
	public function find()
    {
        if (request()->isAjax()) {
		    $data = input('post.');
			$pass=rand(111111, 999999);
			$pass_data = [
				'password'	=> make_Hash($pass)
			];
            $rule=[
				'username'  => 'require|alphaDash|length:5,20'
			];
			$message=[
				'username.require'=>'用户名不能为空',
				'username.length'=>'用户名长度必须在5到20之间'
			];
			$validate=new \think\Validate($rule,$message);
			if(!$validate->check($data)){
				$result = [
					'code' => -1,
					'msg' => $validate->getError()
				];
			}elseif(Users::checkUsername($data['username']) == true){
				$result = [
					'code' => -1,
					'msg' => '用户名不存在'
				];
			}elseif(Users::updateByUsername($data['username'],$pass_data)){
					$info=Users::where('username', '=' ,$data['username'])->find();
					$sub = config('web_webname') . ' - 找回密码';
					if(send_mail($info['mail'], $sub, '系统随机生成的新密码:<font color="red">'.$pass.'</font><br>请尽快登陆修改密码')){
						$result = [
							'code' => 0,
							'msg' => '新的密码已经发送到你绑定的邮箱'
						];
					}else{
						$result = [
							'code' => -1,
							'msg' => '发送邮件失败'
						];
					}
			}else{
				$result = [
					'code' => -1,
					'msg' => '发送邮件失败'
				];
			}
        }

        return $result;
    }
}